package dao;

import java.util.List;

import hibernate.HibernateUtil;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import dto.EmpresaDTO;
import entities.EmpresaEntity;
import entities.ParticularEntity;

public class EmpresaDAO {
	private static EmpresaDAO instancia = null;
	private static SessionFactory sf = null;
	
	private EmpresaDAO(){}
	
	public static EmpresaDAO getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new EmpresaDAO();
		} 
		return instancia;
	}
	
	public void grabarEmpresa(EmpresaEntity empresa){
		Session session = sf.openSession();
		session.beginTransaction();
		session.persist(empresa);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}
	
	public void actualizarEmpresa(EmpresaEntity empresa){
		Session session = sf.openSession();
		session.beginTransaction();
		session.saveOrUpdate(empresa);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public EmpresaEntity getEmpresa(String cuit) 
	{
		Session session = sf.openSession();
		session.beginTransaction();		
		EmpresaEntity e = (EmpresaEntity) session.createQuery("from EmpresaEntity e where e.CUIT = :cuit").setString("cuit", cuit).uniqueResult();
		session.flush();
		session.getTransaction().commit();
		session.close();
		return e;
	}

	public List<EmpresaEntity> getEmpresaList() 
	{
		Session session = sf.openSession();
		session.beginTransaction();
		List<EmpresaEntity> lista = session.createQuery("from EmpresaEntity e").list();
		session.flush();
		session.getTransaction().commit();
		session.close();
		return lista;
	}
}